<?php

function dump($var, $title = false)
{
	echo "<pre style='clear: both; background: #F5F5F5; border: 1px solid brown; padding: 10px; position: relative; z-index: 9999; font-size: 13px; text-align: left;'>";
	if ($title)
		echo "<span style='color: brown; font-family: Verdana; font-size:16px; display: block; padding-bottom: 2px; margin-bottom: 10px; border-bottom: 1px solid brown;'>{$title}</span>";
	var_dump($var);
	echo "</pre>";
}

function generate_named_id($text = null) {
	if(is_null($text))
		return 'id_' . substr(md5(uniqid('id')), -8);

	$text = str_replace(' ', '-', $text);
	$text = strtolower($text);

	$newtext = '';
	$lastaddto = '';
	for($c = 0; $c < strlen($text); $c++) {
		if(preg_match("/[a-zA-Z0-9\-\_]/", $text{$c})) {
			$addto = $text{$c};
			$lastaddto = '';
		} else $addto = '-';

		if($lastaddto != '-')
			$newtext .= $addto;

		$lastaddto = $addto;
	}

	if(substr($newtext, -1) == '-')
		$newtext = substr($newtext, 0, strlen($newtext) - 1);

	return $newtext;
}

function paginate($total, $page) {
	$out = array();

	if($page > $total)
		$page = $total;

	if($total <= 1)
		$out[] = array('value' => 1, 'ugh' => true, 'status' => true);

	else {
		if($page > 3) {
			$out[] = array('value' => 1);
			if($page != 4) $out[] = array('value' => '&hellip;', 'ugh' => true, 'status' => false);
		}

		for($current = $page - 2, $stop = $page + 3; $current < $stop; ++$current) {
			if($current < 1 || $current > $total)
				continue;

			elseif($current != $page)
				$out[] = array('value' => $current);

			else $out[] = array('value' => $current, 'ugh' => true, 'status' => true);
		}

		if($page <= ($total - 3)) {
			if($page != ($total - 3))
				$out[] = array('value' => '&hellip;', 'ugh' => true, 'status' => false);

			$out[] = array('value' => $total);
		}
	}

	return $out;
}